<template>
  <div class="notice-edit-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>{{ isEdit ? '编辑公告' : '添加公告' }}</span>
          <el-button @click="goBack">返回列表</el-button>
        </div>
      </template>
      
      <el-form 
        ref="formRef" 
        :model="form" 
        :rules="rules" 
        label-width="100px" 
        v-loading="loading"
      >
   
        
        <el-form-item label="内容" prop="content">
          <el-input 
            v-model="form.content" 
            type="textarea" 
            :rows="6" 
            placeholder="请输入公告内容"
          />
        </el-form-item>
        
        <el-form-item label="是否置顶" prop="is_sticky">
          <el-switch 
            v-model="form.is_sticky" 
            :active-value="1" 
            :inactive-value="0"
          />
        </el-form-item>
        
        <el-form-item label="有效期" prop="expire_time">
          <el-date-picker
            v-model="form.expire_time"
            type="datetime"
            placeholder="选择过期时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
          <span class="tip-text">不设置则永久有效</span>
        </el-form-item>
        
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">发布</el-radio>
            <el-radio :label="0">草稿</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="submitForm">保存</el-button>
          <el-button @click="goBack">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getNoticeDetail, addNotice, updateNotice } from '@/api/notices'

const route = useRoute()
const router = useRouter()
const formRef = ref(null)
const loading = ref(false)

// 获取路由中的ID参数
const noticeId = computed(() => {
  return parseInt(route.params.id) || 0
})

// 判断是编辑还是新增
const isEdit = computed(() => {
  return noticeId.value > 0
})

// 表单数据
const form = reactive({
  title: '',
  content: '',
  is_sticky: 0,
  expire_time: '',
  status: 1
})

// 表单验证规则
const rules = reactive({
  title: [
    { required: true, message: '请输入公告标题', trigger: 'blur' },
    { max: 100, message: '长度不能超过100个字符', trigger: 'blur' }
  ],
  content: [
    { required: true, message: '请输入公告内容', trigger: 'blur' }
  ]
})

// 返回列表页
const goBack = () => {
  router.push('/notice/list')
}

// 获取公告详情
const getDetail = async () => {
  if (!isEdit.value) return
  
  loading.value = true
  try {
    const res = await getNoticeDetail(noticeId.value)
    if (res.code === 0) {
      // 更新表单数据
      Object.assign(form, res.data)
    } else {
      ElMessage.error(res.message || '获取公告详情失败')
    }
  } catch (error) {
    console.error('获取公告详情失败', error)
    ElMessage.error('获取公告详情失败')
  } finally {
    loading.value = false
  }
}

// 提交表单
const submitForm = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (!valid) return
    
    loading.value = true
    try {
      let res
      if (isEdit.value) {
        // 编辑
        res = await updateNotice(noticeId.value, form)
      } else {
        // 新增
        res = await addNotice(form)
      }
      
      if (res.code === 0) {
        ElMessage.success(`${isEdit.value ? '编辑' : '添加'}成功`)
        goBack()
      } else {
        ElMessage.error(res.message || `${isEdit.value ? '编辑' : '添加'}失败`)
      }
    } catch (error) {
      console.error(`${isEdit.value ? '编辑' : '添加'}失败`, error)
      ElMessage.error(`${isEdit.value ? '编辑' : '添加'}失败`)
    } finally {
      loading.value = false
    }
  })
}

onMounted(() => {
  getDetail()
})
</script>

<style scoped>
.notice-edit-container {
  padding: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.tip-text {
  margin-left: 10px;
  color: #999;
  font-size: 12px;
}
</style> 